<?php

namespace plugins\fund\model;

use think\Db;
use think\Model;

/**
 * 会员模型
 */
class FundModel extends Model
{

    protected $field = true;

    /**
     * 添加流水
     */
    public function addRecord($data = [])
    {
        $member = Db::name('member')->where('uid', $data['uid'])->find();

        $add = [
            'trade_sn'       => $data['trade_sn'], //流水号
            'uid'            => $data['uid'], //用户id
            'nick_name'      => $member['nick_name'], //用户名
            'money'          => $data['money'], //操作金额
            'pay_type'       => $data['pay_type'], //资金类型
            'add_time'       => time(),
            'phone'          => $member['phone'],
            'operate_time'   => time(), //操作时间
            'ip'             => request()->ip(0, true),
            'transaction_id' => '', //交易单号
            'type'           => $data['type'],
            'status'         => $data['status'],
            'other'          => $data['other'],
        ];

        $res = $this->save($add);

        if (!$res) {
            return false;
        }

        return $res; //成功返回自增id
    }

    /**
     * 减少会员金额
     * @param type 1.增加 2.减少
     */
    public function editMoney($uid, $money, $type)
    {
        $member = Db::name('member')->where('uid', $uid)->find();

        if (empty($member)) {
            return ['status' => false, 'message' => '用户不存在', 'code' => 101];
        }

        if ($money == 0) {
            return ['status' => true, 'message' => '成功', 'code' => 200];
        }

        switch ($type) {
            case 1:
                $upd = Db::name('member')->where('uid', $uid)->setInc('money', $money);
                break;
            case 2:
                //判断是否余额不足
                if ($money > $member['money']) {
                    return ['status' => false, 'message' => '账户余额不足', 'code' => 102];
                }
                $upd = Db::name('member')->where('uid', $uid)->setDec('money', $money);
                break;
        }

        if (!$upd) {
            return ['status' => false, 'message' => '失败', 'code' => 101];
        }

        return ['status' => true, 'message' => '成功', 'code' => 200];
    }



    /**
     * 资金冻结或取消冻结
     * @param type 1.冻结 2.取消冻结
     */
    public function frozen($uid, $money, $type)
    {
        $member = Db::name('member')->where('uid', $uid)->find();
        if (empty($member)) {
            return ['status' => false, 'message' => '用户不存在', 'code' => 101];
        }

        switch ($type) {
            case 1:
                //判断是否余额不足
                if ($money > $member['money']) {
                    return ['status' => false, 'message' => '账户余额不足', 'code' => 102];
                }
                $da = [
                    'money' => $member['money'] - $money,
                    'frozen_money' => $member['frozen_money'] + $money,
                ];

                Db::name('member')->where('uid',$uid)->update($da);
                break;
            case 2:
                //判断解冻余额
                if ($money > $member['frozen_money']) {
                    return ['status' => false, 'message' => '账户冻结余额不足,无法解除', 'code' => 102];
                }
                $da = [
                    'money' => $member['money'] + $money,
                    'frozen_money' => $member['frozen_money'] - $money,
                ];
                Db::name('member')->where('uid',$uid)->update($da);
                break;
        }
        return ['status' => true, 'message' => '成功', 'code' => 200];
    }


}
